package com.gxa.learning.portal.service.impl;

import com.gxa.learning.core.pojo.Question;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.portal.mapper.QuestionMapper;
import com.gxa.learning.portal.service.QuestionService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ChenJ
 * @date 2021/12/08 11:06
 **/
@Service("questionService")
public class QuestionServiceImpl extends BaseServiceImpl<Question> implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 根据题库编号获取题目
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getQuestion(Long id) {

        Map<String, Object> map = new HashMap<>(16);

        Integer fullScore = 0;

        Integer typesCount = 0;

        Integer chooseScore = 0;

        Integer judgeScore = 0;

        List<Question> judge = questionMapper.getJudge(id);
        // 如果查询到有判断题,则将判断题添加到map
        if (judge.size() != 0) {
            map.put("judge", judge);
            map.put("judgeCount", judge.size());

            // 题型加一
            typesCount += 1;

            // 将选择题的分值添加到总分
            for (Question question : judge) {
                System.out.println(question);
                // 将判断题的分值添加到总分
                fullScore += question.getCore();
                judgeScore += question.getCore();
            }
            map.put("judgeScore", judgeScore);
        }
        List<Question> choose = questionMapper.getChoose(id);
        // 如果查询到有选择题,则将选择题添加到map
        if (choose.size() != 0) {
            map.put("choose", choose);
            map.put("chooseCount", choose.size());

            // 题型加一
            typesCount += 1;

            for (Question question : choose) {
                // 将选择题的分值添加到总分
                fullScore += question.getCore();
                chooseScore += question.getCore();
            }
            map.put("chooseScore", chooseScore);
        }

        map.put("typesCount", typesCount);

        map.put("fullScore", fullScore);

        return map;
    }

    /**
     * 根据题库编号和学员编号获取学员考试的题目及考试填写的答案
     *
     * @param uId 学员编号
     * @param qId 题库编号
     * @return
     */
    @Override
    public Map<String, Object> getUserQuestion(Long uId, Long qId) {
        Map<String, Object> map = new HashMap<>(16);

        Integer fullScore = 0;

        Integer typesCount = 0;

        Integer chooseScore = 0;

        Integer judgeScore = 0;

        Integer getScore = 0;

        List<Map<String, Object>> userJudge = questionMapper.getUserJudge(uId, qId);
//        List<Question> judge = questionMapper.getJudge(id);
        // 如果查询到有判断题,则将判断题添加到map
        if (userJudge.size() != 0) {
            map.put("judge", userJudge);
            map.put("judgeCount", userJudge.size());

            // 题型加一
            typesCount += 1;

            // 将选择题的分值添加到总分
            for (Map<String, Object> question : userJudge) {
                System.out.println(question);
                // 将判断题的分值添加到总分
                fullScore += Integer.valueOf(question.get("core").toString());
                judgeScore += Integer.valueOf(question.get("core").toString());
                if (question.get("answerFill").equals(question.get("answer"))){
                    getScore += Integer.valueOf(question.get("core").toString());
                }

            }
        }
        List<Map<String, Object>> userChoose = questionMapper.getUserChoose(uId, qId);
//        List<Question> choose = questionMapper.getChoose(id);
        // 如果查询到有选择题,则将选择题添加到map
        if (userChoose.size() != 0) {
            map.put("choose", userChoose);
            map.put("chooseCount", userChoose.size());

            // 题型加一
            typesCount += 1;

            for (Map<String, Object> question : userChoose) {
                // 将选择题的分值添加到总分
                fullScore += Integer.valueOf(question.get("core").toString());
                chooseScore += Integer.valueOf(question.get("core").toString());
                if (question.get("answerFill").equals(question.get("answer"))){
                    getScore += Integer.valueOf(question.get("core").toString());
                }
            }
        }

        map.put("typesCount", typesCount);

        map.put("fullScore", fullScore);

        map.put("getScroe",getScore);

        return map;
    }
}
